package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import java.time.LocalDateTime;

/**
 * 教学子分类实体类
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Getter
@Setter
@ToString(exclude = "category")
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "teaching_subcategories", indexes = {
        @Index(name = "idx_subcategory_category", columnList = "category_id"),
        @Index(name = "idx_subcategory_sort", columnList = "sort_order")
})
public class TeachingSubcategory {

    /**
     * 子分类ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 分类ID
     */
    @NotNull(message = "分类ID不能为空")
    @Column(name = "category_id", nullable = false)
    private Long categoryId;

    /**
     * 子分类名称
     */
    @NotBlank(message = "子分类名称不能为空")
    @Size(max = 100, message = "子分类名称长度不能超过100个字符")
    @Column(nullable = false, length = 100)
    private String name;

    /**
     * 排序顺序
     */
    @Builder.Default
    @Column(name = "sort_order")
    private Integer sortOrder = 0;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_at", updatable = false)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_at")
    private LocalDateTime updatedAt;

    /**
     * 关联关系 - 父分类
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "category_id", insertable = false, updatable = false)
    private TeachingCategory category;

    /**
     * 关联的资源数量（计算字段，非数据库字段）
     */
    @Transient
    private Long resourceCount;

    /**
     * 获取完整路径名称
     */
    public String getFullPathName() {
        if (category != null) {
            return category.getName() + " - " + name;
        }
        return name;
    }
}
